<?php
/**
 * 会员后台控制器
 * 
 * @version $Id: Site.php 408 2012-08-15 09:56:09Z linsir123 $
 */
class controllerSite extends AppController 
{
	/**
	 * @var 模型
	 */
	public $mUser;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		parent::__construct();
		///
		$this->mUser = M::user();
	}
	
	/**
	 * 登录
	 */
	public function actionLogin()
	{
		$this->_checkLogin();
		
		///
		$result = -1;
		if (Request::isPost()) 
			$result = $this->mUser->login();
			
		///
		$this->_assignForm('_user_login', $result);
	}
	
	/**
	 * 注册
	 */
	public function actionRegister()
	{
		$this->_checkLogin();
		
		///
		$result = -1;
		if (Request::isPost()) {
			$con = App::C('Captcha');
			if ($con->check($_POST['captcha'])) 
				$result = $this->mUser->register();
			else 
				$result = '10001|验证码有误';
		}
		
		///
		$this->_assignForm('_user_register', $result);
	}
	
	/**
	 * 验证码
	 */
	function actionCaptcha()
	{
		$con = App::C('Captcha');
		$con->display(1, array('width'=>70, 'height'=>24));
	}
	
	/**
	 * 登出
	 */
	public function actionLogout()
	{
		$this->mUser->logout();
		///
		if (isset($_SERVER['HTTP_REFERER'])) 
			lpUrl::redirect($_SERVER['HTTP_REFERER']);
		else 
			lpUrl::_r();
			
		exit();
	}
	
	/**
	 * 404
	 */
	public function actionMiss()
	{
		///
		lpUrl::_r();
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 验证登录状态
	 */
	private function _checkLogin()
	{
		if ($this->mUser->isLogin()) {
			if (isset($_POST['taobao_url'])) {
				$url = str_replace("e35go", "e35go".$this->mUser->uId, trim($_POST['taobao_url']));
				lpUrl::redirect($url);
			} else 
				lpUrl::_r('user/main');
		}
	}
	
	/**
	 * 输出表单
	 */
	private function _assignForm($tpl, $content)
	{
		if ($content != '10000') {
			if ($content != -1) 
				$this->tipMsg($content);
				
			///
			if ($this->checkCache($tpl)) 
				return ;
				
			///
			AppView::addCss('themes/01/userPannel.css');
			AppView::addJs("jquery.form.js", "jquery.validate.js", 
				"_e35go/plugin/validate.js", "_e35go/plugin/string.js");
		} else 
			$this->_checkLogin();
	}
}
